using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
using System.IO;

using System.Data;

using Pampa.Grid.Utils;

using Pampa.Grid.Core;
using Pampa.Grid.Data;
using Pampa.Grid.UI.Exceptions;
using Pampa.Grid.Exceptions;
using Pampa.Grid.Core.Filters;

using Pampa.WebControls;

namespace Pampa.Grid.UI.CustomControls
{
    [DesignerAttribute(typeof(BaseDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    public class ConfigManager : BaseControl
    {
        private string gridID;
        private ColumnManager columnManager;
        private FilterManager filterManager;

        private Unit tabsHeight;
        private Unit buttonsBarHeight;

        private string pathImages;

        public ConfigManager()
        {
            this.CssClass = "configManagerDefault";
            this.pathImages = "Pampa.Grid" + GetImagesPath();

            tabsHeight = Unit.Pixel(20);
            buttonsBarHeight = Unit.Pixel(24);

            columnManager = new ColumnManager();
            columnManager.Width = Unit.Percentage(95);
            this.Controls.Add(columnManager);

            filterManager = new FilterManager();
            filterManager.Width = Unit.Percentage(95);
            this.Controls.Add(filterManager);
        }

        #region Properties

        public override string ID
        {
            get
            {
                return base.ID;
            }
            set
            {
                base.ID = value;
                columnManager.ID = this.ID + "_columns";
                filterManager.ID = this.ID + "_filters";
            }
        }

        public string GridID
        {
            get 
            { 
                return gridID; 
            }
            set 
            { 
                gridID = value;
                columnManager.GridID = gridID;
                filterManager.GridID = gridID;
            }
        }

        public string ColumnManagerClass
        {
            get { return columnManager.CssClass; }
            set { columnManager.CssClass = value; }
        }

        public string FilterManagerClass
        {
            get { return filterManager.CssClass; }
            set { filterManager.CssClass = value; }
        }

        public string ContextMenuClass
        {
            get { return columnManager.ContextMenuCssClass; }
            set { columnManager.ContextMenuCssClass = value; }
        }

        public Unit TabsHeight
        {
            get { return tabsHeight; }
        }

        public Unit ButtonsBarHeight
        {
            get { return buttonsBarHeight; }
        }

        public string PathImages
        {
            get 
            { 
                return pathImages; 
            }
            set 
            { 
                pathImages = value;
                filterManager.PathImages = value;
                columnManager.PathImages = value;
            }
        }

        #endregion

        #region Load

        protected override void OnLoad(EventArgs e)
        {
            if (this.CssClass == "configManagerDefault")
                RegisterStyleSheet("Pampa.Grid/ConfigManager.css");

            RegisterLibrary("Pampa.Common/Common.js");
            RegisterLibrary("Pampa.Common/TextButton.js");

            RegisterLibrary("Pampa.Grid/Tabs.js");
            RegisterLibrary("Pampa.Grid/ConfigManager.js");

            base.OnLoad(e);
        }

        #endregion

        #region Render

        protected override void RenderContents(HtmlTextWriter writer)
        {
            // Tabs
            RenderTabs(writer);

            // Managers
            RenderManagers(writer);

            // Botonera
            RenderButtonsBar(writer);

            // Definicion
            writer.WriteLine("<script type=\"text/javascript\">");
            writer.WriteLine("var obj" + this.ID + " = new TabsHandler(\"" + this.ID + "\", \"" + this.ID + "_tabs\");");
            writer.WriteLine("obj" + this.ID + ".pathimages = '" + GetResourceUrl(this.PathImages) + "';");
            writer.WriteLine("obj" + this.ID + ".addTab(\"" + Resources.ConfigManager.ColumnManagerTitle + "\",\"" + this.ID + "_contColumnManager\");");
            writer.WriteLine("obj" + this.ID + ".addTab(\"" + Resources.ConfigManager.FilterManagerTitle + "\",\"" + this.ID + "_contFilterManager\");");
            writer.WriteLine("obj" + this.ID + ".setTab(0)");

            writer.WriteLine("ConfigManager.idTabsHandler = \"" + this.ID + "\";");
            writer.WriteLine("ConfigManager.idColumnManager = \"" + columnManager.ID + "\";");
            writer.WriteLine("ConfigManager.idFilterManager= \"" + filterManager.ID + "\";");
            writer.WriteLine("ConfigManager.idGrid = \"" + this.gridID + "\";");

            writer.WriteLine("</script>");
        }

        public void RenderTabs(HtmlTextWriter writer)
        {
            writer.AddAttribute("id", this.ID + "_tabs");
            writer.AddAttribute("class", "tabContainer");
            writer.AddStyleAttribute("height", this.TabsHeight.ToString());
            writer.AddStyleAttribute("width", "100%");

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();   
        }

        private void RenderManagers(HtmlTextWriter writer)
        {
            writer.AddAttribute("id", this.ID + "_managers");
            writer.AddAttribute("class", "managersContainer");
            writer.AddStyleAttribute("height", (this.Height.Value - this.TabsHeight.Value - this.ButtonsBarHeight.Value).ToString() + "px");
            writer.AddStyleAttribute("width", "100%");

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            RenderManager(this.ID + "_contColumnManager", this.columnManager, writer);
            RenderManager(this.ID + "_contFilterManager", this.filterManager, writer);
            writer.RenderEndTag();
        }

        private void RenderManager(string id, Control content, HtmlTextWriter writer)
        {
            writer.AddAttribute("id", id);
            writer.AddStyleAttribute("top", "0px");
            writer.AddStyleAttribute("left", "0px");
            writer.AddStyleAttribute("height", (this.Height.Value - this.TabsHeight.Value - this.ButtonsBarHeight.Value - 10).ToString() + "px");
            writer.AddStyleAttribute("width", "100%");
            writer.AddStyleAttribute("padding", "5px");
            writer.AddStyleAttribute("display", "none");

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            content.RenderControl(writer);
            writer.RenderEndTag();            
        }

        private void RenderButtonsBar(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute("height", this.ButtonsBarHeight.ToString());
            writer.AddStyleAttribute("width", "100%");
            writer.AddAttribute("class", "actionButtonsBar");

            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            // boton de aceptar
            ClientButton btnAccept = new ClientButton();
            btnAccept.MethodName = "ConfigManager.applyChanges";
            btnAccept.CssClass = "button";
            btnAccept.Width = Unit.Pixel(70);
            btnAccept.RollOver = true;
            btnAccept.Text = Resources.ConfigManager.Apply;
            btnAccept.RenderControl(writer);

            writer.RenderEndTag();
        }

        #endregion
    }

}